﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace RuoVea.ExUtil;

/// <summary>
/// 描 述：枚举扩展属性
/// </summary>
public static class EnumExtension
{
    private static Dictionary<string, Dictionary<string, string>> enumCache;

    private static Dictionary<string, Dictionary<string, string>> EnumCache
    {
        get
        {
            if (enumCache == null)
            {
                enumCache = new Dictionary<string, Dictionary<string, string>>();
            }
            return enumCache;
        }
        set { enumCache = value; }
    }

    /// <summary>
    /// 将枚举转换成字典
    /// </summary>
    /// <typeparam name="TEnum">泛型</typeparam>
    /// <returns>获取字典</returns>
    public static Dictionary<string, int> GetDic<TEnum>()
    {
        Dictionary<string, int> dic = new Dictionary<string, int>();
        Type t = typeof(TEnum);
        var arr = Enum.GetValues(t);
        foreach (var item in arr)
        {
            dic.Add(item.ToString(), (int)item);
        }
        return dic;
    }

    /// <summary>
    /// 获得枚举提示文本
    /// </summary>
    /// <param name="en">枚举编码</param>
    /// <returns>枚举提示文本</returns>
    public static string GetEnumText(this Enum en)
    {
        string enString = string.Empty;
        if (null == en) return enString;
        var type = en.GetType();
        enString = en.ToString();
        if (!EnumCache.ContainsKey(type.FullName))
        {
            var fields = type.GetFields();
            Dictionary<string, string> temp = new Dictionary<string, string>();
            foreach (var item in fields)
            {
                var attrs = item.GetCustomAttributes(typeof(TextAttribute), false);
                if (attrs.Length == 1)
                {
                    var v = ((TextAttribute)attrs[0]).Value;
                    temp.Add(item.Name, v);
                }
            }
            EnumCache.Add(type.FullName, temp);
        }
        if (EnumCache[type.FullName].ContainsKey(enString))
        {
            return EnumCache[type.FullName][enString];
        }
        return enString;
    }

    /// <summary>
    /// 返回枚举项的描述信息。
    /// </summary>
    /// <param name="value">要获取描述信息的枚举项。</param>
    /// <returns>枚举想的描述信息</returns>
    public static string GetDescription(this Enum value)
    {
        Type enumType = value.GetType();
        // 获取枚举常数名称。
        string name = Enum.GetName(enumType, value);
        if (name != null)
        {
            // 获取枚举字段。
            FieldInfo fieldInfo = enumType.GetField(name);
            if (fieldInfo != null)
            {
                // 获取描述的属性。
                DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
                    typeof(DescriptionAttribute), false) as DescriptionAttribute;
                if (attr != null)
                {
                    return attr.Description;
                }
            }
        }
        return null;
    }
}
/// <summary>
/// 描 述：文本属性扩展
/// </summary>
public class TextAttribute : Attribute
{
    /// <summary>
    /// 文本属性
    /// </summary>
    /// <param name="value">属性</param>
    public TextAttribute(string value)
    {
        Value = value;
    }
    /// <summary>
    /// 属性字段
    /// </summary>
    public string Value { get; set; }
}
